//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.aidong.web.controller.pt;

import com.aidong.dao.base.ConditionMap;
import com.aidong.dao.base.Pagination;
import com.aidong.dao.wills.DicShopCodeDao;
import com.aidong.dao.wills.MemberInfoDao;
import com.aidong.dao.wills.PtChangeShopDao;
import com.aidong.dao.wills.PtCoachCodeDao;
import com.aidong.dao.wills.PtContractDao;
import com.aidong.model.wills.DicShopCode;
import com.aidong.model.wills.PtChangeShop;
import com.aidong.model.wills.PtCoachCode;
import com.aidong.model.wills.PtContract;
import com.aidong.web.controller.member.PayController;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping({"ptChangeShop"})
public class PtChangeShopController extends PayController {
    @Resource
    PtChangeShopDao ptChangeShopDao;
    @Resource
    PtCoachCodeDao ptCoachCodeDao;
    @Resource
    PtContractDao ptContractDao;
    @Resource
    MemberInfoDao memberInfoDao;
    @Resource
    DicShopCodeDao dicShopCodeDao;

    public PtChangeShopController() {
    }

    @RequestMapping({"list"})
    public ModelAndView list() {
        return new ModelAndView("pt/changeshop/list");
    }

    @RequestMapping({"_list"})
    public ModelAndView _list(@RequestParam(value = "shopCode",required = false) String shopCode, @RequestParam(value = "cotractCode",required = false) String cotractCode, @RequestParam(value = "memberCode",required = false) String memberCode, @RequestParam(value = "coachCode",required = false) String coachCode, @RequestParam(value = "dateFrom",required = false) String dateFrom, @RequestParam(value = "dateTo",required = false) String dateTo, @RequestParam(value = "pageNo",required = false) String pageNo, @RequestParam(value = "totalCount",required = false) String totalCount) {
        Pagination pg = new Pagination(1, 20);
        pg.setPage(this.parseInt(pageNo), this.parseInt(totalCount));
        ConditionMap cd = new ConditionMap();
        this.addConditionShopCode(cd, "b.shopCode", shopCode);
        cd.eq("b.cotractCode", cotractCode);
        cd.eq("b.memberCode", memberCode);
        if(!this.chkNull(coachCode)) {
            ConditionMap mv = new ConditionMap(1);
            mv.eq("b.coachCode", coachCode);
            mv.eq("b.newCoachCode", coachCode);
            cd.addSub(mv);
        }

        if(!this.chkNull(dateFrom)) {
            cd.ge("b.createTime", this.parseDate(dateFrom, "yyyy-MM-dd"));
        }

        if(!this.chkNull(dateTo)) {
            cd.le("b.createTime", this.parseDate(dateTo + " 23:59:59", "yyyy-MM-dd HH:mm:ss"));
        }

        this.ptChangeShopDao.queryPageList(pg, cd);
        ModelAndView mv1 = new ModelAndView("pt/changeshop/_list");
        mv1.addObject("list", pg.getResultList());
        mv1.addObject("pg", pg);
        return mv1;
    }

    @RequestMapping({"add"})
    public ModelAndView add() {
        ModelAndView mv = new ModelAndView("pt/changeshop/add");
        mv.addObject("shopName", this.getTypeList("shopName"));
        mv.addObject("_oper", "add");
        return mv;
    }

    @RequestMapping({"_add"})
    @ResponseBody
    public String _add(@ModelAttribute("changeShop") PtChangeShop changeShop) {
        if(this.chkNull(changeShop.getContractCode())) {
            return this.getResult(false, "请输入合同号");
        } else if(this.chkNull(changeShop.getNewShopCode())) {
            return this.getResult(false, "请选择转入门店");
        } else if(this.chkNull(changeShop.getNewCoachCode())) {
            return this.getResult(false, "请选择新教练");
        } else {
            PtContract oldContract = this.ptContractDao.getByContractCode(changeShop.getContractCode());
            if(oldContract == null) {
                return this.getResult(false, "私教合同号错误!");
            } else if(oldContract.getStatus().intValue() == 1 && oldContract.getOutTime() == null) {
                if(oldContract.getBespeakQuantity().intValue() >= oldContract.getQuantity().intValue()) {
                    return this.getResult(false, "合同已经没有剩余课时");
                } else if(oldContract.getBespeakQuantity() != oldContract.getClearQuantity()) {
                    return this.getResult(false, "有预约课程没有核销,不能转店");
                } else if(oldContract.getEndTime().getTime() < System.currentTimeMillis()) {
                    return this.getResult(false, "合同已过有效期,不能转店");
                } else if(oldContract.getShopCode().equalsIgnoreCase(changeShop.getNewShopCode())) {
                    return this.getResult(false, "转入门店不能和原门店相同");
                } else {
                    DicShopCode shop = (DicShopCode)this.dicShopCodeDao.getById(changeShop.getNewShopCode());
                    if(shop != null && shop.getStatus().intValue() == 1) {
                        PtCoachCode coach = this.ptCoachCodeDao.getByCode(changeShop.getNewCoachCode());
                        if(coach != null && coach.getStatus().intValue() == 1) {
                            int outQuan = oldContract.getQuantity().intValue() - oldContract.getBespeakQuantity().intValue();
                            double outAmount = (double)Math.round((double)outQuan * oldContract.getFactPrice().doubleValue() / (double)oldContract.getQuantity().intValue());
                            changeShop.setShopCode(this.getLoginShopCode());
                            changeShop.setContractCode(oldContract.getContractCode());
                            changeShop.setMemberCode(oldContract.getMemberCode());
                            changeShop.setPtCode(oldContract.getPtCode());
                            changeShop.setCoachCode(oldContract.getCoachCode());
                            changeShop.setOutQuantity(Integer.valueOf(outQuan));
                            changeShop.setOutAmount(Double.valueOf(outAmount));
                            changeShop.setNewShopCode(shop.getCode());
                            changeShop.setNewCoachCode(coach.getCode());
                            changeShop.setCreateUser(this.getLoginUserCode());
                            changeShop.setCreateTime(new Date());
                            changeShop.setUpdateTime(new Date());
                            PtContract contract = new PtContract();
                            contract.setContractCode(oldContract.getContractCode());
                            contract.setShopCode(changeShop.getNewShopCode());
                            contract.setMemberCode(oldContract.getMemberCode());
                            contract.setPtCode(oldContract.getPtCode());
                            contract.setCoachCode(changeShop.getNewCoachCode());
                            contract.setEarnestId((Integer)null);
                            contract.setEarnestAmount(Double.valueOf(0.0D));
                            contract.setInAmount(Double.valueOf(outAmount));
                            contract.setPayAmount(Double.valueOf(0.0D));
                            contract.setSaleType("2");
                            contract.setSaleDate(new Date());
                            contract.setQuantity(Integer.valueOf(outQuan));
                            contract.setFactPrice(Double.valueOf(outAmount));
                            contract.setBespeakQuantity(Integer.valueOf(0));
                            contract.setClearQuantity(Integer.valueOf(0));
                            contract.setBeginTime(this.getTrimDate(new Date()));
                            contract.setEndTime(oldContract.getEndTime());
                            contract.setStatus(Integer.valueOf(1));
                            contract.setCommissionDate(new Date());
                            contract.setReceivePaper(Integer.valueOf(0));
                            contract.setSignStatus(Integer.valueOf(2));
                            contract.setPrintLabel(Integer.valueOf(0));
                            contract.setOldContractCode(oldContract.getContractCode());
                            String saleClassify = "2";
                            Integer pn = this.ptContractDao.getCountByMemberCode(contract.getMemberCode());
                            if(!this.chkNullZ(pn)) {
                                saleClassify = "1";
                            }

                            Date dt = this.memberContractDao.getFirstDateByMemberCode(contract.getMemberCode());
                            if(this.getAccountDate(new Date()).equals(this.getAccountDate(dt))) {
                                saleClassify = "3";
                            }

                            contract.setSaleClassify(saleClassify);
                            contract.setCreateUser(this.getLoginUserCode());
                            contract.setCreateTime(new Date());
                            contract.setUpdateTime(new Date());
                            contract.setReviewStatus(Integer.valueOf(0));
                            contract.setSalesCode(oldContract.getSalesCode());
                            contract.setSalesCode2(oldContract.getSalesCode2());

                            try {
                                this.ptChangeShopDao.addChangeShop(changeShop, contract);
                                this.addUserLog("私教转店", "合同号:" + changeShop.getContractCode() + ",会员:" + changeShop.getMemberCode());
                            } catch (Exception var13) {
                                this.log.error("add changeShop", var13);
                                return this.getResult(false, "保存发生错误");
                            }

                            return this.getResult(true, "保存完成");
                        } else {
                            return this.getResult(false, "转入教练错误");
                        }
                    } else {
                        return this.getResult(false, "转入门店错误");
                    }
                }
            } else {
                return this.getResult(false, "无效合同");
            }
        }
    }

    @RequestMapping({"getCoachList"})
    @ResponseBody
    public String getCoachList(@RequestParam("shopCode") String shopCode) {
        List list = this.ptCoachCodeDao.getShopCoachList(shopCode);
        StringBuilder sb = new StringBuilder();
        sb.append("[");

        PtCoachCode coach;
        for(Iterator i$ = list.iterator(); i$.hasNext(); sb.append("{code:\"").append(this.convStr(coach.getCode())).append("\"").append(",name:\"").append(this.convStr(coach.getChName())).append("\"").append("}")) {
            coach = (PtCoachCode)i$.next();
            if(sb.length() > 5) {
                sb.append(",");
            }
        }

        sb.append("]");
        return this.getSuccess(sb.toString(), "");
    }
}
